package com.lyc.service.impl;

import com.lyc.DTO.ItemTypeDTO;
import com.lyc.mapper.ItemTypesMapper;
import com.lyc.pojo.ItemType;
import com.lyc.service.ItemTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemTypeServiceImpl implements ItemTypeService {
    
    @Autowired
    private ItemTypesMapper itemTypesMapper;
    
    // 获取分类列表
    @Override
    public List<ItemType> getItemTypes() {
        return itemTypesMapper.getItemTypes();
    }
    
    // 添加分类
    @Override
    public void addItemType(ItemTypeDTO itemTypeDTO) {
        // 验证必要字段
        if (itemTypeDTO.getCategoryId() == null || itemTypeDTO.getCategoryId() <= 0) {
            throw new IllegalArgumentException("所属版块ID(categoryId)不能为空且必须大于0");
        }
        
        if (itemTypeDTO.getName() == null || itemTypeDTO.getName().trim().isEmpty()) {
            throw new IllegalArgumentException("类型名称(name)不能为空");
        }
        
        ItemType itemType = new ItemType();
        BeanUtils.copyProperties(itemTypeDTO, itemType);
        itemTypesMapper.addItemType(itemType);
    }
    
    // 更新分类
    @Override
    public void updateItemType(ItemTypeDTO itemTypeDTO) {
        // 验证必要字段
        if (itemTypeDTO.getId() == null || itemTypeDTO.getId() <= 0) {
            throw new IllegalArgumentException("分类ID(id)不能为空且必须大于0");
        }
        
        if (itemTypeDTO.getCategoryId() == null || itemTypeDTO.getCategoryId() <= 0) {
            throw new IllegalArgumentException("所属版块ID(categoryId)不能为空且必须大于0");
        }
        
        if (itemTypeDTO.getName() == null || itemTypeDTO.getName().trim().isEmpty()) {
            throw new IllegalArgumentException("类型名称(name)不能为空");
        }
        
        ItemType itemType = new ItemType();
        BeanUtils.copyProperties(itemTypeDTO, itemType);
        itemTypesMapper.updateItemType(itemType);
    }
    
    // 删除分类
    @Override
    public void deleteItemType(Integer id) {
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("分类ID(id)不能为空且必须大于0");
        }
        itemTypesMapper.deleteItemType(id);
    }
    
    // 更新分类状态
    @Override
    public void updateItemTypeStatus(Integer id, String status) {
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("分类ID(id)不能为空且必须大于0");
        }
        
        if (status == null || status.trim().isEmpty()) {
            throw new IllegalArgumentException("状态(status)不能为空");
        }
        
        itemTypesMapper.updateItemTypeStatus(id, status);
    }
}